/**
 * Kohive.app.TasksController
 * @extends ExtMVC.Controller
 * Manages the addition and removal of tasks
 */
Kohive.app.TasksController = Ext.extend(Kohive.controllers.KohiveController, {
	name			: 'tasks',

	/**
	 * 
	 */
	files			: {},

	/**
	 * 
	 */
	milestones		: {},

	constructor		: function(config) {
		var config = config || {};

		// Get the OS and the controller
		var os = ExtMVC.OS.getOS();
		var controller = this;

		Kohive.app.TasksController.superclass.constructor.call(this, {
					viewsPackage	: Kohive.views.tasks
				});

		this.actsAsKohiveController(Kohive.models.Task);

		/**
		* @action create 
		* Creates a new model instance, displays errors if required and redirects to index
		*/
		this.registerAction('create', function(form, date) {
					// form.el.mask('Saving...', 'x-mask-loading');
					this.onCreate(form, date);
				});

		Ext.apply(this, {
					/**
					 * Called when the save button on an edit form is pressed.  By default this will load the model object with the form values,
					 * then call save() on the model object.  Override onCreateSuccess and onCreateFailure to update success and failure callbacks
					 * @param {Ext.form.BasicForm} form A reference to the FormPanel
					 */
					onCreate		: function(form, date) {
						this.newModelObj = new this.model({});

						this.form = form;
						var date = (date.format) ? date.format('j M y') : date;

						this.onSave(form, this.newModelObj, {
									success	: this.onCreateSuccess,
									failure	: this.onCreateFailure
								}, date);
					},

					/**
					* Used by both onCreate and onUpdate.  Don't override this unless you understand what you're doing
					*/
					onSave			: function(form, model, options, date) {
						var v = this.indexWindow;
						v.showSubmitting();

						// Get the values from the form
						var values = form.getValues();
						values.due = date;
						values.task_type = values.type;

						// change the array into a string
						if (values.tag_list && Ext.isArray(values.tag_list))
							values.tag_list = values.tag_list.join(' ');

						// Get the current membership id
						values.author_membership_id = Kohive.OS
								.getCurrentHive().get('id');

						/**
						 * Updates the model (which also saves it).  Uses success and failure options passed in from
						 * one of onUpdate or onCreate, depending on which one called this
						 */
						model.update(values, Ext.apply({}, options, {
											scope		: this,
											url			: this.saveUrl,
											callback	: function() {
												form.el.unmask();
											}
										}));
					},

					/**
					* Called after successful item creation.  By default this just first onSaveSuccess
					*/
					onCreateSuccess	: function() {
						this.onSaveSuccess();

						var tempTask = new Ext.util.DelayedTask(function() {
									var field = this.form.items.items[2];
									field.focus();
								}, this);
						tempTask.delay(1000);
					}
				});
	},

	getMilestones	: function() {
		if (this.milestones[Kohive.OS.getCurrentHiveId()]) {
			this.fireEvent('milestones', this.milestones[Kohive.OS
							.getCurrentHiveId()]);
			return;
		};

		// get all the files
		var store = Kohive.models.Milestone.findAll({
			url			: Kohive.OS.scopedUrl('milestones'),
			listeners	: {
				scope	: this,
				'load'	: function(store) {
					// create a array with the default 'none' value
					var milestones = [[null,
							Ext.ux.Translator.translate('none'), null]];

					// loop through each of the store items
					store.each(function(record) {
								milestones.push([record.get('id'),
										record.get('title'),
										record.get('due_at')]);
							}, this);

					// create and return the store
					var milestonesStore = new Ext.data.SimpleStore({
								fields	: ['id', 'title', 'due_at'],
								data	: milestones
							});

					this.milestones[Kohive.OS.getCurrentHiveId()] = milestonesStore;

					this.fireEvent('milestones', this.milestones[Kohive.OS
									.getCurrentHiveId()]);
				}
			}
		}
		);
	}
}
);

ExtMVC.OS.getOS().registerController('tasks', Kohive.app.TasksController);